package com.ratta.supernote.note.plugincore.modules;

import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.ratta.supernote.note.plugincore.NativeEventSpec;
import com.ratta.supernote.note.plugincore.api.NoteDataCacheApi;
import com.ratta.supernote.note.plugincore.api.NoteLibAPI;
import com.ratta.supernote.note.plugincore.api.PluginEntranceAPI;
import com.ratta.supernote.note.plugincore.constant.paramkey.PluginMenuItemKey;
import com.ratta.supernote.note.plugincore.core.PluginAppAPI;
import com.ratta.supernote.note.plugincore.ui.menu.PluginMenuItemShowData;
import com.ratta.supernote.note.plugincore.utils.SystemUtils;
import com.ratta.supernote.note.plugincore.utils.UiHandler;

import org.jetbrains.annotations.Nullable;

public class RTNEvent extends NativeEventSpec {
    public static String NAME = "RTNEvent";
    PluginAppAPI pluginApp;

    public RTNEvent(ReactApplicationContext reactContext, PluginAppAPI pluginApp) {
        super(reactContext);
        this.pluginApp = pluginApp;
    }

    @Override
    @NonNull
    public String getName() {
        return NAME;
    }

    @Override
    public void onMounted() {
        Log.i(NAME,"pluginApp mounted");
        if (pluginApp != null) {
            pluginApp.onMounted();
        }

    }

    @Override
    public void onClick(double x, double y) {
        pluginApp.onClick();

    }

    @Override
    public void onStop() {
        Log.i(NAME, "RTNEvent onStop");
        if (pluginApp != null) {
            pluginApp.onStop();
        }
    }

    @Override
    public void getOrientation(Promise promise) {
        WindowManager windowManager = (WindowManager) getCurrentActivity()
                .getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            Display display = windowManager.getDefaultDisplay();
            promise.resolve(display.getRotation());
        } else {
            promise.reject(new Throwable("windowManager is null"));
        }

    }

/*    @Override
    public void dismissLassoBar() {
        pluginApp.onDismissLassoBar();

    }*/




    @Override
    public void registerMenuItemRes(double type, ReadableMap menuItem, @Nullable ReadableMap showData, Promise promise) {
        PluginEntranceAPI.getInstance().registerMenuItemRes(pluginApp, type, menuItem, showData, promise);


    }

    @Override
    public void registerSubMenuItemRes(ReadableMap menuItem, @Nullable ReadableMap showData, Promise promise) {
        PluginEntranceAPI.getInstance().registerSubMenuItemRes(pluginApp, menuItem, showData, promise);

    }

    @Override
    public void unregisterMenuItemRes(double id, Promise promise) {

        PluginEntranceAPI.getInstance().unregisterPluginMenuItem((int) id, pluginApp.getPluginID());
        promise.resolve(true);


    }


    @Override
    public void modifyMenuItem(ReadableMap menuItem, ReadableMap showData, Promise promise) {
        PluginEntranceAPI.getInstance().modifyMenuItem(pluginApp, menuItem, showData, promise);


    }

    @Override
    public void setLassoBoxSate(double state, Promise promise) {
        if(NoteDataCacheApi.getInstance() == null) {
            promise.resolve(false);
            Log.e(NAME,"NoteDataCacheApi instance is null");
            return;
        }
        NoteDataCacheApi.getInstance().getLassoState().postValue((int) state);
        promise.resolve(true);

    }


    /**
     * Register configuration button
     * @param showData
     * @param promise
     */
    @Override
    public void registerConfigButtonRes(ReadableMap showData, Promise promise) {
        if(showData == null) {
            promise.resolve(false);
            Log.e(NAME,"registerConfigButtonRes showData is null!");
            return;
        }
        if(showData.hasKey(PluginMenuItemKey.showDataRegionType)) {
            PluginMenuItemShowData pluginMenuItemShowData = new PluginMenuItemShowData();
            pluginMenuItemShowData.setRegionType(showData.getInt(PluginMenuItemKey.showDataRegionType));
            if(showData.hasKey(PluginMenuItemKey.showDataRegionWidth)) {
                pluginMenuItemShowData.setRegionWidth(showData.getInt(PluginMenuItemKey.showDataRegionWidth));
            } else {
                pluginMenuItemShowData.setRegionWidth(0);
            }
            if(showData.hasKey(PluginMenuItemKey.showDataRegionHeight)) {
                pluginMenuItemShowData.setRegionHeight(showData.getInt(
                        PluginMenuItemKey.showDataRegionHeight
                ));
            } else {
                pluginMenuItemShowData.setRegionHeight(0);
            }
            if(pluginApp != null) {
                pluginApp.registerConfigButtonRes(pluginMenuItemShowData);

            }

            promise.resolve(true);
        } else {
            Log.e(NAME,"registerConfigButtonRes showData is error!");
            promise.resolve(false);
        }


    }

    /**
     * Show interface
     * @param showData Interface data, cannot be null, can only set 2 and 3
     *       {
     *       regionType: Plugin display area type
     *             1: Display as dialog
     *             2: Display fullscreen
     *       regionWidth: Region width
     *       regionHeight: Region height
     *       }
     * @param promise
     */
    @Override
    public void showPluginView(ReadableMap showData, Promise promise) {
        if(showData == null) {
            promise.resolve(false);
            return;
        }

        try {
            if (showData.hasKey(PluginMenuItemKey.showDataRegionType)) {
                int type = showData.getInt(PluginMenuItemKey.showDataRegionType);
                int regionWidth = showData.getInt(PluginMenuItemKey.showDataRegionWidth);
                int regionHeight = showData.getInt(PluginMenuItemKey.showDataRegionHeight);
                if(regionHeight <=0 || regionWidth <=0) {
                    promise.reject(new Throwable("width or height is Wrong!"));
                    return;
                }
                PluginMenuItemShowData menuShowData = new PluginMenuItemShowData(type, regionWidth, regionHeight);
                UiHandler.getInstance().handler().post(new Runnable() {
                    @Override
                    public void run() {
                        pluginApp.showPluginView(menuShowData);
                    }
                });
                promise.resolve(true);
            }
        } catch (Exception e) {
            promise.reject(e);
        }



    }

    @Override
    public void closePluginView(Promise promise) {
        UiHandler.getInstance().handler().post(new Runnable() {
            @Override
            public void run() {
                pluginApp.stopPlugin();

            }
        });

        promise.resolve(true);

    }

    /**
     * Set slide bar status
     * @param status true for slidable, false for not slidable
     */
    @Override
    public void setSlideBarStatus(boolean status, Promise promise) {

        NoteLibAPI.getInstance().setSlideBarStatus(getCurrentActivity(), status, pluginApp.getName());
        promise.resolve(true);

    }

    @Override
    public void invalidatePluginView() {
        if(pluginApp != null) {
            Log.i(NAME,"invalidatePluginView");
            pluginApp.invalidatePluginView();
        }
    }

    @Override
    public void setSystemDormancyState(boolean enable) {
        SystemUtils.setSystemDormancyState(getCurrentActivity(),enable, "Plugin_Sticker");
    }

    @Override
    public void setStatusBarAndSlideBarState(boolean isLock) {
        SystemUtils.setStatusBarAndSlideBarState(getCurrentActivity(), isLock, "Plugin_Sticker");
    }
}
